
import mapTool from './mapTool.js'

cc.Class({
    extends: cc.Component,

    properties: {
        // map:cc.TiledMap,
        mapNode: cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
          //开启物理引擎 要放在onLoad中
          cc.director.getPhysicsManager().enabled = true;
          cc.director.getPhysicsManager().debugDrawFlags = 1;
          cc.director.getPhysicsManager().gravity = cc.v2(0,0);
    },

    start () {
        //动态加载地图
        this.initMap();
      
    },

    initMap(){
        // let mapArr = [['00000','01000','00000'],['00010','11110','00100'],['00000','10000','00000']];
        let mapArr =mapTool.getRandomMapName();
        console.log('mapArr',mapArr)
        // mapArr = mapArr.reverse();
        //地图的数据是从上到下，从左到右 但是cocos的坐标系是从下到上，从左到右，所有反转了一下数组便于理解
        // console.log(mapArr,'resve')
        let startPos =cc.v2(0,0);
        // console.log('startPos',startPos)
        let offsetX= 384;
        let offsetY =384;
        let mapCount = 0;
        //更加mapArr一个二维数组，动态加载TiledMap地图
        for(let i = 0; i < mapArr.length; i++) {
           for(let j = 0; j < mapArr[i].length; j++) {
               let mapName = mapArr[i][j];
               mapCount++;
               if(mapName =='00000'||!mapName) continue;
               cc.loader.loadRes(`map/${mapName}`,cc.TiledMapAsset,(err,assets) =>{
                    if(err)console.log('err');
                    let node = new cc.Node();
                   
                   let map = node.addComponent(cc.TiledMap);
                   map.tmxAsset = assets;
                  
                   node.setPosition(cc.v2(j*offsetX,-i*offsetY));
                   node.parent = this.mapNode;
                   node.anchorX = 0;
                   node.anchorY = 0;
            
                   this.initMapNode(node);

                   mapCount --;
                                    
               })
           }
        }
       
     

        //new Node() 给node动态添加上TiledMap组件 ，设置TiledMap Tmx Asset

        //设置node的锚点和位置

        // //给一个加载页
    
    },

    initMapNode(tiledMapNode){

        let tiledMap = tiledMapNode.getComponent(cc.TiledMap);
        let tiledSize = tiledMap.getTileSize();
        //获取tiledMap的 wall层级 将级分组设置为wall
        //遍历wall层级的每一块tile，为其动态添加上刚体组件和物理碰撞组件
         let wallLayer = tiledMap.getLayer('wall');
         let layerSize = wallLayer.getLayerSize();
        //  console.log('layerSize',layerSize)
         for(let i = 0; i < layerSize.width; i++){
             for(let j = 0; j < layerSize.height; j++){
               let tiled =  wallLayer.getTiledTileAt(i,j,true);
               if(tiled.gid != 0){
                   tiled.node.group ='wall';
                //  NO tile.node.addComponent(cc.PhysicsBoxCollider);
                //  tile.node.getComponent(cc.PhysicsBoxCollider);

                //出现了Tiledmap
                 let body = tiled.node.addComponent(cc.RigidBody);
                 body.type = cc.RigidBodyType.Static;
                 let collider = tiled.node.addComponent(cc.PhysicsBoxCollider);
                 collider.offset = cc.v2(tiledSize.width / 2, tiledSize.height / 2);
                 collider.size = tiledSize;
                 collider.apply();
               }
           
             }
         }
    }

    // update (dt) {},
});
